<template>
	<view class="form">
		<view class="form-main">
			<view class="opt">
				<input v-model="account" type="text" placeholder="请输入账号" class="inp">
			</view>
			<view class="opt">
				<input v-model="password" type="password" placeholder="请输入密码" class="inp">
			</view>
			<view class="opt">
				<view class="button" @click="loginFn">点击登录</view>
			</view>
			<view class="opt" style="text-align: center;">
				<view>更多登录方式</view>
			</view>
			<view class="opt">
				<navigator url="/pages/register/register" style="color: #666;">未有账号,跳转注册</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				account: "",
				password: "",
			}
		},
		methods: {
			async loginFn() {
				if (this.account == "" || this.password == "") {
					uni.showToast({
						title: "输入框不能为空",
						icon: "none"
					})
					return false
				}

				if (!(/\w+@qq\.com/.test(this.account))) {
					uni.showToast({
						title: "请输入邮箱格式的账号",
						icon: "none"
					})
					return false;
				}


				let that = this;
				await uniCloud.callFunction({
						name: "login",
						data: {
							account: this.account,
							password: this.password
						}
					})
					.then(
						resData => {
							console.log(resData)
							if (resData.result.code == 200) {
								uni.showToast({
									title: "登录成功"
								})

								// 先本地存储数据
								uni.setStorageSync("INFO", resData.result.info)

								// 延迟300毫秒跳转个人中心
								setTimeout(() => {
									uni.switchTab({
										url: "/pages/mine/mine"
									})
								}, 300)
							} else {
								uni.showToast({
									title: "登录失败",
									icon: "error"
								})
							}
						}
					)
					.catch(
						err => {
							console.log(err)

						}
					)
			}
		},
		onLoad(options) {

		}
	}
</script>


<style lang="scss">
	@import "../../static/scss/variable.scss";

	.form {
		padding: $padd;
	}

	.form-main {
		height: 800rpx;
		width: 100%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;

		.opt {
			width: 100%;
			margin-bottom: 20rpx;

			.inp {
				width: 100%;
				height: 80rpx;
				border-bottom: 1px solid #ccc;
			}

			.button {
				padding: 15rpx;
				border: 1px solid #ccc;
				text-align: center;
				border-radius: 10rpx;
			}
		}
	}
</style>